<template>
  <div class="doctor-detail-page">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="header-content">
        <div class="back-btn" @click="$router.go(-1)">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <path d="M15 18l-6-6 6-6"/>
          </svg>
        </div>
        <div class="header-title">医生详情</div>
        <div class="home-btn" @click="$router.push('/')">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/>
            <polyline points="9,22 9,12 15,12 15,22"/>
          </svg>
        </div>
      </div>
    </div>

    <!-- 医生信息 -->
    <div v-if="doctor" class="doctor-info-section">
      <div class="doctor-avatar-large">
        <img 
          :src="doctor.avatar" 
          :alt="doctor.name"
          @error="handleImageError"
        />
      </div>
      
      <div class="doctor-basic-info">
        <h1 class="doctor-name">{{ doctor.name }}</h1>
        <p class="doctor-title">{{ doctor.title }}</p>
        <p class="doctor-department">{{ doctor.department }}</p>
        <p class="doctor-hospital">{{ doctor.hospital }}</p>
      </div>

      <div class="doctor-rating-section">
        <div class="rating-display">
          <svg width="20" height="20" viewBox="0 0 24 24" fill="#FFD700" stroke="#FFD700" stroke-width="2">
            <polygon points="12,2 15.09,8.26 22,9.27 17,14.14 18.18,21.02 12,17.77 5.82,21.02 7,14.14 2,9.27 8.91,8.26"/>
          </svg>
          <span class="rating-score">{{ doctor.rating.toFixed(1) }}</span>
          <span class="rating-count">({{ doctor.reviewCount }} 评价)</span>
        </div>
      </div>

      <div class="doctor-details">
        <div class="detail-item">
          <h3>专业经验</h3>
          <p>{{ doctor.experience }}</p>
        </div>
        
        <div class="detail-item">
          <h3>咨询费用</h3>
          <p class="price">¥{{ doctor.price }}</p>
        </div>
        
        <div class="detail-item">
          <h3>下一个可用时间</h3>
          <p>{{ doctor.nextAvailableTime }}</p>
        </div>
      </div>

      <div class="action-buttons">
        <button class="select-btn" @click="selectDoctor">
          选择此医生
        </button>
        <button class="back-btn-secondary" @click="$router.go(-1)">
          返回
        </button>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-else-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>正在加载医生信息...</p>
    </div>

    <!-- 错误状态 -->
    <div v-else class="error-container">
      <p class="error-text">医生信息加载失败</p>
      <button @click="retryLoad" class="retry-btn">重试</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()

interface Doctor {
  _id: string
  name: string
  department: string
  avatar: string
  rating: number
  reviewCount: number
  nextAvailableTime: string
  price: number
  experience: string
  hospital: string
  title: string
}

const doctor = ref<Doctor | null>(null)
const loading = ref(true)

const loadDoctorDetail = async () => {
  loading.value = true
  
  try {
    // 从本地存储获取医生信息
    const savedDoctor = localStorage.getItem('selectedDoctor')
    if (savedDoctor) {
      doctor.value = JSON.parse(savedDoctor)
    } else {
      // 如果没有本地存储，使用模拟数据
      doctor.value = {
        _id: route.params.id as string,
        name: '程晓波',
        department: '儿科',
        avatar: 'https://randomuser.me/api/portraits/men/1.jpg',
        rating: 4.8,
        reviewCount: 456,
        nextAvailableTime: '12月23日 上午08:30',
        price: 350,
        experience: '15年临床经验',
        hospital: '北京儿童医院',
        title: '主任医师'
      }
    }
  } catch (error) {
    console.error('加载医生详情失败:', error)
  } finally {
    loading.value = false
  }
}

const selectDoctor = () => {
  if (doctor.value) {
    // 存储选中的医生信息
    localStorage.setItem('selectedDoctorId', doctor.value._id)
    localStorage.setItem('selectedDoctor', JSON.stringify(doctor.value))
    
    // 返回医生选择页面
    router.push('/doctor-select')
  }
}

const retryLoad = () => {
  loadDoctorDetail()
}

const handleImageError = (event: Event) => {
  const img = event.target as HTMLImageElement
  img.src = ''
}

onMounted(() => {
  loadDoctorDetail()
})
</script>

<style scoped>
.doctor-detail-page {
  min-height: 100vh;
  background: #f8f9fa;
}

.header {
  background: #1877F2;
  padding: 0 20px;
  padding-top: env(safe-area-inset-top);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
}

.back-btn, .home-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 8px;
  transition: all 0.2s;
}

.back-btn:hover, .home-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.header-title {
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  flex: 1;
  text-align: center;
}

.doctor-info-section {
  padding: 20px;
  background: white;
  margin: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.doctor-avatar-large {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  overflow: hidden;
  margin: 0 auto 20px;
  background: linear-gradient(135deg, #00D4FF 0%, #5B73FF 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.doctor-avatar-large img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.doctor-basic-info {
  text-align: center;
  margin-bottom: 20px;
}

.doctor-name {
  font-size: 24px;
  font-weight: 700;
  color: #333;
  margin: 0 0 8px 0;
}

.doctor-title {
  font-size: 16px;
  color: #1877F2;
  font-weight: 600;
  margin: 0 0 4px 0;
}

.doctor-department {
  font-size: 14px;
  color: #666;
  margin: 0 0 4px 0;
}

.doctor-hospital {
  font-size: 14px;
  color: #999;
  margin: 0;
}

.doctor-rating-section {
  display: flex;
  justify-content: center;
  margin-bottom: 24px;
}

.rating-display {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f8f9fa;
  padding: 8px 16px;
  border-radius: 20px;
}

.rating-score {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.rating-count {
  font-size: 14px;
  color: #666;
}

.doctor-details {
  margin-bottom: 24px;
}

.detail-item {
  margin-bottom: 16px;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.detail-item:last-child {
  border-bottom: none;
}

.detail-item h3 {
  font-size: 14px;
  color: #999;
  margin: 0 0 4px 0;
  font-weight: 500;
}

.detail-item p {
  font-size: 16px;
  color: #333;
  margin: 0;
  font-weight: 500;
}

.detail-item .price {
  color: #1877F2;
  font-weight: 600;
  font-size: 18px;
}

.action-buttons {
  display: flex;
  gap: 12px;
}

.select-btn {
  flex: 1;
  height: 48px;
  background: #1877F2;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.select-btn:hover {
  background: #1565c0;
}

.back-btn-secondary {
  height: 48px;
  padding: 0 20px;
  background: #f5f5f5;
  color: #666;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.back-btn-secondary:hover {
  background: #e0e0e0;
  color: #333;
}

.loading-container, .error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #1877F2;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-text {
  color: #dc3545;
  margin-bottom: 16px;
  font-size: 16px;
}

.retry-btn {
  background: #1877F2;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.retry-btn:hover {
  background: #1565c0;
}
</style> 